package com.example.state;

import android.content.Context;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManager;

import com.example.bounce.Bounce;
import com.example.glsurfaceview.MyGLSurfaceView;
import com.example.object.GameShape;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

/**
 * Created by Lucas on 06/06/13.
 */
public class AppState extends TimerTask{

    /**
     * Home state Id
     */
    public final static int HOME = 0;
    /**
     * Achievement state Id
     */
    public final static int ACHIEVEMENT = 1;
    /**
     * World state Id
     */
    public final static int WORLD = 2;
    /**
     * Ball state Id
     */
    public final static int BALL = 3;
    /**
     * InGame state Id
     */
    public final static int INGAME = 4;
    /**
     * Exit Id
     */
    public final static int EXIT = -1;
    /**
     * Null state Id
     */
    public final static int NULL = -2;
    /**
     * Width of the android screen device
     */
    public static int DEVICE_WIDTH;
    public static final float SIMULATION_WIDTH =2f;
    /**
     * Height of the android screen device
     */
    public static int DEVICE_HEIGHT;
    public static float SIMULATION_HEIGHT;
    public static float RATIOHEIGHT;
    /**
     * The debug TAG
     */
    private final String TAG = "AppState";
    /**
     * Boolean to know if the current appState is running
     */
    private boolean _isRunning;
    /**
     * The current state
     */
    protected int _currentState;
    /**
     * Boolean to know if the list of gameShape is initialized
     */
    protected boolean _isInitiate;
    /**
     * Context of the application
     */
    protected Context _context;
    /**
     * A link to the bounce application
     */
    protected Bounce _bounce;
    /**
     * The list of the game shape in the state
     */
    protected List<GameShape> _listGameShape;
    /**
     * The GLSurfaceView of the application
     */
    protected MyGLSurfaceView _glView;

    /**
     * The constructor
     * @param context the context of the application
     * @param glView the GLSurfaceView
     */
    public AppState(Context context, MyGLSurfaceView glView) {
        super();
        // get and set the screen size of the android device
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        DEVICE_HEIGHT = size.y;
        DEVICE_WIDTH  = size.x;
        RATIOHEIGHT = ((1.0f*size.y)/(1.0f*size.x));
        SIMULATION_HEIGHT = 2f*RATIOHEIGHT;

        _context = context;
        _bounce = (Bounce) context;
        _listGameShape = new ArrayList<GameShape>();
        _isInitiate = false;
        _glView = glView;
        _isInitiate = false;
    }

    /**
     * Handle the touch event
     * @param e
     * @return
     */
    public boolean onTouchEvent(MotionEvent e){
        return true;
    }

    /**
     * calculate the next coordinate of all the gameShape
     */
    public void calculateNextFrame() {

    }

    /**
     * Draw the GameShape of all the objects in the list
     */
    public void draw() {
        if (!_listGameShape.isEmpty()){
            for(int i=0; i<_listGameShape.size(); i++){
                _listGameShape.get(i).draw();
            }
        }
    }

    /**
     * define a method to set the opengl handler.
     * @param aPositionHandle
     * @param aTextureHandle
     * @param uMVPMatrixHandle
     * @param vMatrix
     * @param projMatrix
     */
    public void setGLHandler(int aPositionHandle, int aTextureHandle, int uMVPMatrixHandle, float[] vMatrix, float[] projMatrix) {
        Log.i(TAG, "Call to setGLHandler");
        for(int i=0; i<_listGameShape.size(); i++){
            _listGameShape.get(i).setGLHandler(aPositionHandle, aTextureHandle, uMVPMatrixHandle, vMatrix, projMatrix);
        }
        _isInitiate = true;
    }

    /**
     * define a mathod to initiate the game shapes
     */
    public void initGameShape(){
    }

    /**
     * remove all the object in the list of game shape
     */
    public void clearGameShape(){
        // TODO difference between clear and new ?
        _listGameShape.clear();
        _isInitiate = false;
    }

    /**
     * return either if the list of object is initialized
     * @return
     */
    public boolean isInitiate() {
        return _isInitiate;
    }

    /**
     * define a method to handle the key down event
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    /**
     * return if the timertask is running
     * @return
     */
    public boolean isRunning(){
        return _isRunning;
    }

    @Override
    public boolean cancel() {
        _isRunning = false;
        return super.cancel();
    }

    /**
     * calculate the next state of the game
     */
    @Override
    public void run() {
        _isRunning = true;
        calculateNextFrame();
        _glView.requestRender();
    }

    public String toString(){
        switch(_currentState){
            case HOME:
                return "Home";
            case ACHIEVEMENT:
                return "Achievement";
            case WORLD:
                return "World";
            case BALL:
                return "Ball";
            case INGAME:
                return "In Game";
            default:
                return "None";
        }
    }

    public void setState(int nextState) {
        _currentState = nextState;
    }

    public List<GameShape> getList() {
        return _listGameShape;
    }
}
